# coding=utf-8

import datetime
import os
from git.repo import Repo
from utils import Codezip


def git(projectinfo, taskinfo):
    date = datetime.datetime.now().strftime('%Y%m%d%H')
    codepath = '/Users/dayang/PycharmProjects/Devops/codes/' + date + '/' + taskinfo.uid + \
               '/' + 'codes'
    scriptpath = '/Users/dayang/PycharmProjects/Devops/codes/' + date + '/' + taskinfo.uid + \
                 '/' + 'scripts'
    taskpath = '/Users/dayang/PycharmProjects/Devops/codes/' + date + '/' + taskinfo.uid
    if os.path.exists(taskpath):
        Repo.clone_from(url=projectinfo.repo, to_path=codepath, branch=taskinfo.tag)  # 下载代码
        before_download = os.path.join(scriptpath, "before_download.sh")
        with open(before_download, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.before_download)
        after_download = os.path.join(scriptpath, "after_download.sh")
        with open(after_download, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.after_download)
        before_deploy_sc = os.path.join(scriptpath, "before_deploy_sc.sh")
        with open(before_deploy_sc, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.before_deploy_sc)
        after_deploy_sc = os.path.join(scriptpath, "after_deploy_sc.sh")
        with open(after_deploy_sc, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.after_deploy_sc)
        filepath = Codezip(taskpath, taskpath)
        return str(filepath)
    else:
        os.makedirs(taskpath)
        os.makedirs(codepath)
        os.makedirs(scriptpath)
        Repo.clone_from(url=projectinfo.repo, to_path=codepath, branch=taskinfo.tag)  # 下载代码
        before_download = os.path.join(scriptpath, "before_download.sh")
        with open(before_download, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.before_download)
        after_download = os.path.join(scriptpath, "after_download.sh")
        with open(after_download, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.after_download)
        before_deploy_sc = os.path.join(scriptpath, "before_deploy_sc.sh")
        with open(before_deploy_sc, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.before_deploy_sc)
        after_deploy_sc = os.path.join(scriptpath, "after_deploy_sc.sh")
        with open(after_deploy_sc, mode='w', encoding='utf-8') as f:
            f.write(taskinfo.after_deploy_sc)
        filepath = Codezip(taskpath, taskpath)
        return str(filepath)
        # print("line25server:%s" % prinfo.servers)
    # print(filepath)
    # Exists = os.path.exists(path)
    # print(" lin 19 exits status %s" % Exists)
    # if os.path.exists(path):
    #     print("21:path：%s" % path)
    #     print("文件夹存在删除掉")
    #     serverlist = prinfo.servers.split(',')
    #     # for host in serverlist:
    #     #     hostinfo = Hosts.query.filter_by(hostname=host).first()
    #     #     with SSHProxy(hostname=host, port=hostinfo.port, username=hostinfo.username,
    #     #                   password=hostinfo.passwd) as ssh:
    #     # ssh.upload(filepath, prinfo.path)
    #     # os.remove(path)
    # else:
    #     print("文件夹不存在")
    #     print("32:path：%s" % path)
    #     os.makedirs(path)
    #     Repo.clone_from(url=prinfo.repo, to_path=path, branch=taskinfo.tag)
    #     codezip(path, path)
    #     # print(prinfo.servers)
    #     serverlists = prinfo.servers.split('', )
    #     # print(type(serverlists))
    #     # for server in serverlists:
    #     #     print(server)
    #     #     print(type(server))
